package com.topboya.linker.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.topboya.linker.R;
import com.topboya.linker.model.levelitem.SelectDeviceGroup;
import com.topboya.linker.model.response.OrganizationResopnse;
import com.topboya.linker.ui.activity.SeeVistorActivity;
import com.topboya.linker.ui.activity.xSelectVistorActivity;
import com.topboya.linker.ui.adapter.RecycleViewDivider;
import com.topboya.linker.ui.adapter.xAuditedAdapter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.base.fragment.BaseFragment;
import com.topboya.linker.ui.presenter.MyCustomerPresenter;
import com.topboya.linker.ui.view.MyCustomerView;
import com.topboya.linker.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit2.http.POST;


public class AuditedFragment extends BaseFragment<MyCustomerView, MyCustomerPresenter> implements MyCustomerView{

    private static final String TAG = "AuditedFragment";
    private RecyclerView ryc_audited;
    private  int pageNO =1;
    private List<OrganizationResopnse.DocDetailInfoList> docDetailInfoLists;
    private xAuditedAdapter xAuditedAdapter;
    private SmartRefreshLayout smart_refreshlayout;
    @Override
    protected int getLayoutId() {

        return R.layout.fragment_audited;
    }

    @Override
    protected void initView() {

        ryc_audited = mRootView.findViewById(R.id.ryc_audited);
        smart_refreshlayout = mRootView.findViewById(R.id.smart_refreshlayout);
        smart_refreshlayout.setEnableRefresh(false);
    }

    @Override
    protected void initData() {

        initRecycleView();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        LogUtils.v(TAG +"onActivityCreated");
    }

    @Override
    public void onStart() {
        super.onStart();
        LogUtils.v(TAG +"onStart");
    }

    private void initRecycleView() {

        mPresenter.getOrgList(String.valueOf(pageNO),null,"0");
        docDetailInfoLists = new ArrayList<>();
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        ryc_audited.setLayoutManager(layoutManager);
        ryc_audited.addItemDecoration(new RecycleViewDivider(getActivity(), LinearLayoutManager.HORIZONTAL, 2, 0xBFC0C2));
        xAuditedAdapter = new xAuditedAdapter(getActivity(),docDetailInfoLists);
        ryc_audited.setAdapter(xAuditedAdapter);

    }
    @Override
    public void initListener() {

        xAuditedAdapter.setOnItemClickListener((v, position) -> {

            SelectDeviceGroup selectDeviceGroup = new SelectDeviceGroup();
            selectDeviceGroup.setId(docDetailInfoLists.get(position).getId());
            selectDeviceGroup.setOrgName(docDetailInfoLists.get(position).getOrgName());

            Intent intent = new Intent(mContext,SeeVistorActivity.class);
            intent.putExtra("DocDetailInfo",selectDeviceGroup);

//        标记是是否是创建任务页面还是我的页面里面进入的，有的布局需要隐藏
            intent.putExtra("isCreateTask",false);
            mContext.startActivity(intent);
        });
        smart_refreshlayout.setOnLoadmoreListener(refreshlayout -> {

            pageNO++;
            mPresenter.getOrgList(String.valueOf(pageNO),null,"0");

        });
    }
    @Override
    protected MyCustomerPresenter createPresenter() {


        return new MyCustomerPresenter((BaseActivity) getActivity());
    }


    @Override
    public void setCustomeData(ArrayList<OrganizationResopnse.DocDetailInfoList> groupHList) {

        if (null !=groupHList && groupHList.size() >0){

            docDetailInfoLists.addAll(groupHList);
            xAuditedAdapter.notifyDataSetChanged();
            smart_refreshlayout.finishLoadmore();
        }


    }

    @Override
    public void getOrgListInAudited(ArrayList<OrganizationResopnse.DocDetailInfoList> groupHList) {


    }
}
